package com.stxx.assistant.framwork.utils.dbmeta;

import cn.hutool.core.util.StrUtil;
import org.noear.solon.annotation.Component;

/**
 * @author Fuzhenlei
 * @date 2025/01/14 11:33
 */
@Component
public class MysqlMeta implements DbMeta {
    @Override
    public String getTableSql(String schema) {
        return "SELECT table_name" +
                ",table_comment " +
                "FROM information_schema.tables " +
                "WHERE table_schema = '"+schema+"'";
    }

    @Override
    public String getColumnSql(String schema,String tableName) {
        return "SELECT COLUMN_NAME column_name\n" +
                ",COLUMN_COMMENT column_comment\n" +
                ",data_type data_type\n" +
                ",IfNULL(CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION) len\n" +
                ",NUMERIC_SCALE decimals\n" +
                ",case when COLUMN_KEY='PRI' then 1 else 0 end is_pk \n"+
                "from information_schema.columns \n" +
                "where table_schema = '"+schema+"' and table_name='"+tableName+"';";
    }

    @Override
    public String getJavaType(String type) {
        if(StrUtil.containsAnyIgnoreCase(type,"int")){
            return "int";
        }else if(StrUtil.containsAnyIgnoreCase(type,"double","decimal","float")){
            return "decimal";
        }else if(StrUtil.containsAnyIgnoreCase(type,"bit")){
            return "bool";
        }else if(StrUtil.containsAnyIgnoreCase(type,"date","timestamp")){
            return "datetime";
        }
        return "string";
    }

    @Override
    public String getDbName() {
        return "MYSQL";
    }
}
